07. Control Freak

Obviously our video still isn’t very sophisticated, but we can make it a whole lot better by adding in some controls using scripts.

Create a VideoControl.cs file, and add in the following script, which will allow the video to toggle between play and pause when the spacebar is pressed. Add the script to your sphere and assign the Audio Source variable.

Now obviously the notion of a spacebar doesn’t exist in Cardboard, but as we are building it up, the spacebar is a great way to test and make sure things work on your computer, before you deploy to Cardboard.

Unity 2017 Video Player class information

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class VideoControl : MonoBehaviour {

    private UnityEngine.Video.VideoPlayer videoPlayer;


    [SerializeField]
    private AudioSource audioSource;



    void Start () {
        videoPlayer = GetComponent<UnityEngine.Video.VideoPlayer> ();


        if (videoPlayer.clip != null) 
        {
            videoPlayer.EnableAudioTrack (0, true);
            videoPlayer.SetTargetAudioSource(0, audioSource);
        }
    }

    //Check if input keys have been pressed and call methods accordingly.
    void Update(){
        //Play or pause the video.
        if (Input.GetKeyDown ("space")) 
        {
            if (videoPlayer.isPlaying)
                videoPlayer.Pause ();
            else
                videoPlayer.Play();
                audioSource.Play();
        }

    }
}